<script setup>
import useCityStore from "@/store/modules/city";
import CityGroup from "./cpns/city-group.vue";

const router = useRouter();
const searchValue = ref("");
// 搜索功能
const cancelClick = () => {
  router.back();
};
// tab切换
const tabActive = ref();
// 从store中获取数据
const cityStore = useCityStore();
cityStore.fetchAllCitiesData();
const { allCities } = storeToRefs(cityStore); // 使用storeToRefs包裹数据才是响应式的
// 获取选中标签后的数据
// const currentGroup = allCities.value[tabActive.value]; // 不是响应式，所以必须使用computed
const currentGroup = computed(() => allCities.value[tabActive.value]);
</script>

<template>
  <div class="city top-page">
    <div class="top">
      <van-search
        v-model="searchValue"
        placeholder="城市/区域/位置"
        shape="round"
        show-action
        @cancel="cancelClick"
      />
      <van-tabs v-model:active="tabActive" color="#ff9854" line-height="2">
        <!-- 遍历一个对象 -->
        <template v-for="(value, key, index) in allCities" :key="key">
          <!-- 这里绑定了name=key，那么tabActive绑定的就不是默认的索引 -->
          <van-tab :title="value.title" :name="key"></van-tab>
        </template>
      </van-tabs>
    </div>
    <div class="content">
      <!-- <city-group :group-data="currentGroup" /> -->
      <template v-for="(value, key, index) in allCities">
        <city-group v-show="tabActive === key" :group-data="value" />
      </template>
    </div>
  </div>
</template>

<style lang="less" scoped>
.city {
  .top {
    position: relative;
    z-index: 9;
  }
  // 布局滚动
  .content {
    height: calc(100vh - 98px);
    overflow-y: auto;
  }
}
</style>
